import requests
import re

#获取页面函数
def getHtmlText(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""
#解析页面函数
def parsePage(ilt,page):
    try:
        tlt = re.findall(r'\"title\":\".*?\"',page)
        plt = re.findall(r'\"price\":\"[\d\.]*\"',page)
        for i in range(len(tlt)):
            price = eval(plt[i].split(':')[1])
            title = eval(tlt[i].split(':')[1])
            ilt.append([price,title])
    except:
        print("")
#展示信息
def printGoodsList(ilt):
    tplt = "{:4}\t{:8}\t{:16}"
    #print(tplt.format("序号","商品名称","价格"))
    count = 0
    for g in ilt:
        count = count + 1
        print(tplt.format(count,g[1],g[0]))

def main():
    goods = "鼠标"
    depth = 1
    start_url = "https://s.taobao.com/search?q=" + goods

    infuList = []
    for i in range(depth):
        try:
            url = "https://bangumi.bilibili.com/jsonp/seasoninfo/1586.ver?callback=seasonListCallback&jsonp=jsonp&_=1509534711369"
            html = getHtmlText(url)
            print(html)
            parsePage(infuList,html)
        except:
            continue
    printGoodsList(infuList)
main()